﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using SageKing.AspNetCore.Extensions;
using SageKing.Core.Contracts;

namespace SageKing.AspNetCore;

public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            var response = new ActionResponse()
            {
                Code = 400,
                Message = "Validation failed",
                Errors = context.ModelState.GetAllErrors()
            };
            context.Result = new BadRequestObjectResult(response);
        }
    }
}